//
//  SYIMStudentsView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/8/27.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYIMStudentsView.h"

#import "SYIMStudentsTableViewCell.h"


@interface SYIMStudentsView () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *curTableView;
@property (strong, nonatomic) NSMutableArray *dataArr;
@property (strong, nonatomic) NSMutableArray *contDataArr;//总学生数据源

@property (strong, nonatomic) SYNoLabel *noLabel;//没有学生


@end


@implementation SYIMStudentsView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.curTableView.delegate = self;
    self.curTableView.dataSource = self;
    self.curTableView.tableFooterView = [UIView new];
    [self.curTableView registerCellWithNIbNames:@[@"SYIMStudentsTableViewCell"]];
    self.dataArr = [NSMutableArray array];
    
    self.noLabel = [[SYNoLabel alloc] initWithNameStr:@"暂无学生" contView:self];
    
    
}

- (void)getGroupMembersStudent:(NSString *)groupId {
    
//    [SYRequestAPI bb_TutorServicesSetTutorStudentInfoTutorId:groupId success:^(NSMutableArray * dataListArr) {
//
//    } fail:^(NSError *error) {
//
//    }];
    
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI bb_TutorServicesSetTutorStudentInfoTutorId:groupId success:^(NSMutableArray * _Nonnull dataListArr) {
        weakSelf.contDataArr = dataListArr;
        
    } fail:^(NSError *error) {
        
        
    }];
    
}
//进入学生的id
- (void)setEnterStr:(NSString *)enterStr {
    //判断进入学生的id是否与学生列表的学生idi一致，如果一致，就存入数据源。刷新表
    for (SYInteractionStudentModel *studentModel in self.contDataArr) {
        if ([studentModel.UserIMID isEqualToString:enterStr]) {
            if (self.dataArr.count > 0) {
                for (SYInteractionStudentModel *stModel in self.dataArr) {//不同入口进来的id去重
                    if ([stModel.UserIMID isEqualToString:enterStr]) {
                        return;
                    } else {
                        [self.dataArr addObject:studentModel];
                        [self.curTableView reloadData];
                        return;
                    }
                    
                }
            } else {
                [self.dataArr addObject:studentModel];
                [self.curTableView reloadData];
                return;
            }
            break;
        }
    }
}


//退出学生的id
- (void)setQuitStr:(NSString *)quitStr {
    for (SYInteractionStudentModel *studentModel in self.contDataArr) {
        if ([studentModel.UserIMID isEqualToString:quitStr]) {
            [self.dataArr removeObject:studentModel];
            [self.curTableView reloadData];
            break;
        }
    }
}

- (void)isFids:(NSString *)fid isSpeak:(NSInteger)isSpeak {
    
    if (fid.length == 0) {
        return;
    }
    
    for (SYInteractionStudentModel *studentModel in self.dataArr) {
        if ([studentModel.UserIMID isEqualToString:fid] || [studentModel.UserId isEqualToString:fid]) {
            studentModel.isMake = isSpeak;
            
            [self.curTableView reloadData];
            break;
        }
    }
    
}


#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYIMStudentsTableViewCell *studentsCell = [tableView dequeueReusableCellWithIdentifier:@"SYIMStudentsTableViewCell" forIndexPath:indexPath];
    
    studentsCell.selectionStyle = UITableViewCellSelectionStyleNone;
    SYInteractionStudentModel *interactionModel = self.dataArr[indexPath.row];
    studentsCell.nameLab.text = interactionModel.StudentName;
    if (interactionModel.isMake) {
        [studentsCell.microphoneBtn setImage:[UIImage imageNamed:@"board_fy"] forState:UIControlStateNormal];
    } else {
        [studentsCell.microphoneBtn setImage:[UIImage imageNamed:@"board_fy_not"] forState:UIControlStateNormal];
    }
    __weak typeof(self) weakSelf = self;
    studentsCell.interactionModel = interactionModel;
    studentsCell.imStudentsBlock = ^(SYInteractionStudentModel * _Nonnull interaction) {
        [weakSelf.curTableView reloadData];
        if ([self.delegate respondsToSelector:@selector(imStudentsViewInteractionStudentModel:reserved:)]) {
            [self.delegate imStudentsViewInteractionStudentModel:interaction reserved:self];
        }
    };
    
    return studentsCell;
}


@end























